package com.ricervcvcer.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ricervcvcer.common.util.ResultEntity;
import com.ricervcvcer.user.entity.SysRight;
import com.ricervcvcer.user.service.ISysRightService;
import com.ricervcvcer.user.service.ISysRoleRightService;
import com.ricervcvcer.user.util.RicervcvcerConstant;
import com.ricervcvcer.user.vo.MenuNode;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author moge
 * @since 2022-01-10
 */
@RestController
@RequestMapping("right")
public class SysRightController {

    @Autowired
    private ISysRightService rightService;
    @Autowired
    ISysRoleRightService roleRightService;

    @ApiOperation("获取所有菜单信息")
    @GetMapping("menu")
    public List<MenuNode> menus(HttpServletRequest request){
        return rightService.menus(request);
    }

    @ApiOperation("获取所有菜单信息")
    @GetMapping
    public ResultEntity all(){
        List<MenuNode> menus = rightService.menus();
        return ResultEntity.successWithData(menus);
    }
    @ApiOperation("获取所有菜单信息")
    @GetMapping("permis")
    public ResultEntity allPermis(){
        List<MenuNode> menus = rightService.menusPermis();
        return ResultEntity.successWithData(menus);
    }
    @ApiOperation("获取菜单文件夹类型")
    @GetMapping("folders")
    public ResultEntity folders(){
        List<MenuNode> menus = rightService.folders();
        return ResultEntity.successWithData(menus);
    }


    @ApiOperation("根据菜单编码查询信息")
    @GetMapping("selectById/{rightCode}")
    public ResultEntity selectById(@PathVariable String rightCode){
        HashMap map = new HashMap<>();
        map.put("right_code",rightCode);
        List<SysRight> rights = rightService.listByMap(map);
        return ResultEntity.successWithData(rights.get(0));
    }

    @ApiOperation("删除一条菜单信息")
    @RequestMapping("del/{rightCode}")
    public ResultEntity delete (@ApiParam("要删除的菜单编号") @PathVariable String rightCode){
        // 删除相关关系
        Map map=new HashMap<>();
        map.put("rf_right_code",rightCode);
        List list1 = roleRightService.listByMap(map);
        boolean b=true;
        if(list1.size()!=0){
            b = roleRightService.removeByMap(map);
        }

        // 判断是否存在子菜单
        QueryWrapper qw=new QueryWrapper();
        qw.eq("right_parent",rightCode);
        List list = rightService.list(qw);
        if(list.size()!=0){
            throw new RuntimeException("请先删除子菜单！");
        }

        // 删除
        QueryWrapper<SysRight> qw2=new QueryWrapper();
        qw2.eq("right_code",rightCode);
        boolean remove = rightService.remove(qw2);
        if(!remove && !b){
            throw new RuntimeException("删除失败！");
        }
        return ResultEntity.successWithoutData();
    }

    @ApiOperation("修改一条菜单信息")
    @PostMapping("update")
    public ResultEntity update (@ApiParam("要删除的菜单编号") @RequestBody SysRight sysRight){
        QueryWrapper qw=new QueryWrapper();
        qw.eq("right_code",sysRight.getRightCode());
        boolean update = rightService.update(sysRight,qw);
        if(!update){
            throw new RuntimeException(RicervcvcerConstant.UPDATE_ERROR);
        }
        return ResultEntity.successWithoutData();
    }

    @ApiOperation("添加一个菜单")
    @PostMapping("add")
    public ResultEntity add(@RequestBody SysRight sysRight){
        // 判断当前菜单编号是否已经存在
        String rightCode = sysRight.getRightCode();
        QueryWrapper qw=new QueryWrapper();
        qw.eq("right_code",rightCode);
        SysRight one = rightService.getOne(qw);
        if(!StringUtils.isEmpty(one)){
            throw new RuntimeException("这个菜单编号已经被使用！");
        }

        // 添加菜单
        boolean save = rightService.save(sysRight);
        if(!save){
            throw new RuntimeException(RicervcvcerConstant.ADD_ERROR);
        }
        return ResultEntity.successWithoutData();
    }
}
